package com.sgolub.commanndsonline.dao.entity;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.hibernate.Query;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.sgolub.commanndsonline.domain.entity.AdvertisingRegistry;
import com.sgolub.commanndsonline.domain.sec.SecUser;

/**
 * @author sgolub
 *
 */
@Repository
public class AdvertisingRegistryDAOImpl implements AdvertisingRegistryDAO {

    @Autowired
    private SessionFactory sessionFactory;
    
	public AdvertisingRegistry get(Integer id) {
		AdvertisingRegistry advertisingRegistry = (AdvertisingRegistry)sessionFactory.
								getCurrentSession().load(AdvertisingRegistry.class, id);
		return advertisingRegistry;
	}

	@SuppressWarnings("unchecked")
	public AdvertisingRegistry	getItemByInventarNr(String inventarNr){
		String sql = "from AdvertisingRegistry where inventarNr = '" + inventarNr + "'";
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		List<AdvertisingRegistry> list = queryH.list();
		
		if (list.size() > 1) {
			System.out.println("AN EEROR OCCURED MORE THAN 2 Users with SAME username FOUND");
			return null;
		}
		
		AdvertisingRegistry advertisingRegistry;
		
		if (list.size()>0){
			 advertisingRegistry = (AdvertisingRegistry)list.get(0);	
		} else {
			 advertisingRegistry = new AdvertisingRegistry();
		}
		
		return advertisingRegistry;
	}
	
	public void add(AdvertisingRegistry advertisingRegistry) {
		sessionFactory.getCurrentSession().save(advertisingRegistry);

	}

	public void update(AdvertisingRegistry advertisingRegistry) {
		sessionFactory.getCurrentSession().update(advertisingRegistry);
	}

	public void remove(Integer id) {
		// TODO Auto-generated method stub

	}

	@SuppressWarnings("unchecked")
	public List<AdvertisingRegistry> list() {
		return sessionFactory.getCurrentSession().createQuery("from AdvertisingRegistry").list();
	}

	@SuppressWarnings("unchecked")
	public List<AdvertisingRegistry> filter() {
		return sessionFactory.getCurrentSession().createQuery("from AdvertisingRegistry").list();
	}

	
	
	/* (non-Javadoc)
	 * @see com.sgolub.commanndsonline.dao.entity.AdvertisingRegistryDAO#advansedListWithPagination
	 * @param 
	 * int rp, 
	 * String sortorder, 
	 * int page, 
	 * String query, 
	 * String sortname, 
	 * String qtype
	 * 
	 */
	@SuppressWarnings("unchecked")
	public List<AdvertisingRegistry> advansedListWithPaginationFlexGrid(int rp, String sortorder, 
			int page, String query, String sortname, String qtype ){
//		List<AdvertisingRegistry> result = new ArrayList<AdvertisingRegistry>();
		String sqlFrom = "from AdvertisingRegistry";
		String sqlWhere = "";
		String sqlOrder = "";
		String sql = "";
		
		if (query.length()>0 && qtype.length()>0) {
			sqlWhere = " where " + qtype + " like '%" + query + "%' ";
		}
		
		if (sortname.length()>0 && sortorder.length()>0) {
			sqlOrder = " order by " + sortname + " " + sortorder;
		}
		
		sql = sqlFrom + sqlWhere + sqlOrder;
		
		
		System.out.println("Query = " + sql);
		
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		queryH.setFirstResult(rp * (page-1));
		queryH.setMaxResults(rp);
		
		return queryH.list();
	}
	
    @SuppressWarnings({ "rawtypes", "unchecked" })
	public List<AdvertisingRegistry> advansedListWithPaginationJQGrid
	(int rowsPerPage, int currentPage, String sortByColumn, String sortMode, 
			HashMap<String, String> filtersMap) {
    	System.out.println("== AdvertisingRegistryDAOImpl.advansedListWithPaginationJQGrid ==");
    	System.out.println("== PASSED FILTERS ==");
    	// Get a set of the entries 
    	Set set = filtersMap.entrySet(); 
    	// Get an iterator 
    	Iterator i = set.iterator(); 
    	// Display elements 
    	while(i.hasNext()) { 
	    	Map.Entry me = (Map.Entry)i.next(); 
	    	System.out.print(me.getKey() + ": "); 
	    	System.out.println(me.getValue()); 
    	} 
    	
		String sqlFrom = "from AdvertisingRegistry";
		String sqlWhere = " where 1=1 ";
		String sqlOrder = "";
		String sql = "";
		
		if (sortByColumn.length()>0 & sortMode.length()>0){
			
			sqlOrder = " order by " + sortByColumn + " " + sortMode;
		}
		
		
		
		System.out.println("== PARSED WHERE ==");
		if ( filtersMap.get("supportType").length() > 0 ){
			System.out.println(" and upper(supportTypeName)  like upper('%" + filtersMap.get("supportType") + "%') ");
			sqlWhere+=" and upper(supportTypeName)  like upper( '%" + filtersMap.get("supportType") + "%') "; 
			}
		
		if ( filtersMap.get("regionName").length() > 0 ){
			System.out.println(" and upper(regionName)  like upper('%" + filtersMap.get("regionName") + "%') ");
			sqlWhere+=" and upper(regionName)  like upper('%" + filtersMap.get("regionName") + "%') ";
			}

		if ( filtersMap.get("localityName").length() > 0 ){
			System.out.println(" and upper(localityName)  like upper('%" + filtersMap.get("localityName") + "%') ");
			sqlWhere+=" and upper(localityName)  like upper('%" + filtersMap.get("localityName") + "%') ";
			}		
    	
		if ( filtersMap.get("addresString").length() > 0 ){
			System.out.println(" and upper(adress)  like upper('%" + filtersMap.get("addresString") + "%') ");
			sqlWhere+=" and upper(adress ) like upper('%" + filtersMap.get("addresString") + "%') ";
			}		
		
		if ( filtersMap.get("light").length() > 0 & filtersMap.get("light").equals("true") ){
				System.out.println(" and light = 'true'  ");
			sqlWhere+=" and light = 'true'  ";
			}		
		
		if ( filtersMap.get("priceFrom").length() > 0 ){
			System.out.println(" and price_base  >= " + filtersMap.get("priceFrom") + " ");
			sqlWhere+=" and price_base  >= " + filtersMap.get("priceFrom") + " ";
			} 
		
		if ( filtersMap.get("priceTo").length() > 0 ){
			System.out.println(" and price_base  <= " + filtersMap.get("priceTo") + " ");
			sqlWhere+=" and price_base  <= " + filtersMap.get("priceTo") + " ";
			}
		
		System.out.println("== SQL WHERE ==");
		System.out.println(sqlWhere);
    	
		
		sql = sqlFrom + sqlWhere + sqlOrder;
		
		System.out.println("Query = " + sql);
		
		Query queryH = sessionFactory.getCurrentSession().createQuery(sql);
		
		return queryH.list();
		
    }
	
	
}
